package com.os.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.os.service.DeskServiceS;
import com.os.serviceimpl.DeskServiceImplS;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class AddCaiPinControllerS extends HttpServlet{
	DeskServiceS ds = new DeskServiceImplS();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf8");
		int did = Integer.parseInt(req.getParameter("did"));
		int oid = Integer.parseInt(req.getParameter("oid"));
		int fnum = ds.findFnum(oid);
		String a =  req.getParameter("data");
		JSONArray s = JSONArray.fromObject(a); 
		StringBuffer foodlist = new StringBuffer();
		for (int i = 0; i < s.size(); i++) {
			JSONObject jj = JSONObject.fromObject(s.get(i));
			if(i == 0){
				foodlist.append(jj.getString("fid"));
			}else{
				foodlist.append(","+jj.getString("fid"));
			}
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String ftime = df.format(new Date());
		System.out.println(ftime);
		HttpSession session = req.getSession();
		int wid = (int) session.getAttribute("uid");
		boolean flag = ds.addDesksS(did, foodlist.toString(), wid,fnum,ftime);
		PrintWriter pw = resp.getWriter();
		JSONObject j = new JSONObject();
		j.put("flag", flag);
		pw.write(j.toString());
		pw.flush();
		pw.close();
	}
}
